【Java实验】类与对象

您所在的位置:网站首页 work out和work it out的区别 【Java实验】类与对象

【Java实验】类与对象

2023-04-02 19:02| 来源: 网络整理| 查看: 265

目录

1. 建立复数类

2. 建立Line类

1. 建立复数类

在课堂示例的基础上,显示复数时如果虚部为0时只显示实部,实部为0时只显示虚部,虚部为负数时以a-bi的形式显示,并为复数类增加减法功能。

package work;

import java.util.Scanner;

public class Work4 {

    private int real;

    private int imag;

    public Work4(int real, int imag) {

        this.real = real;

        this.imag = imag;

    }

    public void display() {

        if (real == 0 && imag == 0) {

            System.out.println("0");

        } else if (real == 0) {

            System.out.println(imag + "i");

        } else if (imag == 0) {

            System.out.println(real);

        } else if (imag > 0) {

            System.out.println(real + "+" + imag + "i");

        } else {

            System.out.println(real + "-" + (-imag) + "i");

        }

    }

    public static void main(String[] args) {

        Scanner real=new Scanner(System.in);

        Scanner imag=new Scanner(System.in);

        Work4 scanner = new Work4(real.nextInt(),imag.nextInt());

        scanner.display();

    }

}

2. 建立Line类

计算机上的线实际上是线段,要求包含两个端点;颜色为彩虹色;线的粗细是类变量,至少包含show方法。

package work;

import java.util.Scanner;

public class Work5 {

    public static void main(String[] args) {         // TODO Auto-generated method stub         LineSet q=new LineSet();         Scanner in=new Scanner(System.in);         System.out.println("请输入第一个点的x:");         q.pointA_x = in.nextInt();         System.out.println("请输入第一个点的y:");         q.pointA_y = in.nextInt();         System.out.println("请输入第二个点的x:");         q.pointB_x = in.nextInt();         System.out.println("请输入第二个点的y:");         q.pointB_y = in.nextInt();                 System.out.println("请输入一个数字表示颜色(0-3):");         q.color= in.nextInt();         System.out.println("请输入一个数字表示粗细(0:粗 1:细):");         q.boldsize = in.nextInt();         q.show();     }

} class LineSet {     public int pointA_x, pointA_y, pointB_x, pointB_y;     public int color;     public static int boldsize;

    public String res_color_effect(int color)     {         String color_effect = null;         if(color == 0)             color_effect ="red";         else if(color == 1)                     color_effect ="blue";                 else if(color == 2)                     color_effect ="yellow";                 else if(color == 3)                     color_effect ="green";                 return color_effect;             }              public static String bold_effect;     public String res_bold_effect(int boldsize)     {         if(boldsize == 0){             bold_effect = "粗";         } else if(boldsize == 1){             bold_effect = "细";         }         return bold_effect;     }     public void show()      {         System.out.println("线段从点A"+"("+pointA_x+","+pointA_y+")"+"到点B("+pointB_x+","+pointB_y+")"+"颜色:"+res_color_effect(color)+"粗细:"+res_bold_effect(boldsize));          } }



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3